#include "order.h"

long order_form = 0;
int file_flag = 0;

int ORDER_sand(char *buf, char *number, FILE *fp)
{
    struct device *p = head; // 遍历链表
    while (p != NULL)
    {
        if (strstr(p->device_name, number)) // 找到设备
        {
            printf("找到%s设备%d,开始转发\n", p->device_name, p->socket);
            // 发送数据
            int size = write(p->socket, buf, strlen(buf));
            if (size < 0)
            {
                printf("发送失败\n");
                fprintf(fp, "fail\n");
                return -1;
            }
            printf("发送的数据 %s :", buf);
            printf("发送的数据大小 %d\n", size);
            break;
        }

        p = p->next;
    }
    return 0;
}

int generate_random_number()
{
    // 初始化随机数生成器
    srand(time(NULL));

    // 生成一个6位数的随机数
    int random_number = rand() % 900000 + 100000;

    return random_number;
}

int ORDER_RDWR(char *buf)
{
    char number[128] = {0};
    char commodity_num[128] = {0};
    char num[128] = {0};
    FILE *fp;
    if (file_flag == 0)
    {
        fp = fopen("/root/kkcode/second_item/Data/order/Simple_order.txt", "w");
        if (fp == NULL)
        {
            printf("打开文件失败\n");
            return -1;
        }
    }
    else
    {
        fp = fopen("/root/kkcode/second_item/Data/order/Simple_order.txt", "a+");
        if (fp == NULL)
        {
            printf("打开文件失败\n");
            return -1;
        }
    }
    int set = ORDER_str(buf, number, commodity_num, num);
    if (set == 0)
    {
        file_flag = 1;
        // 获取时间一起写入
        fprintf(fp, "%s,%s,%s", number, commodity_num, num);
        fclose(fp);
        return 1;
    }
    else if (set == 1)
    {
        char Buf[128] = {0};
        pthread_mutex_lock(&keep_lock);
        order_form = generate_random_number();
        sprintf(Buf, "ok,%ld\n", order_form);
        if (ORDER_sand(Buf, number, fp) == false)
        {
            return -1;
        }
        printf("订单创建成功\n");
        fclose(fp);
        return 2;
    }
    else if (set == 2)
    {
        file_flag = 0;
        pthread_mutex_unlock(&keep_lock);
        fprintf(fp, "%s,%ld,%s\n", commodity_num, order_form,num);
        printf("交易成功\n");
        fclose(fp);
        sleep(1);
        order_str();
        
        return 0;
    }
    else if (set == 3)
    {
        file_flag = 0;
        pthread_mutex_unlock(&keep_lock);
        printf("取消订单\n");
        int set = fprintf(fp, "cancel\n");
        if (set < 0)
        {
            printf("写入失败\n");
            fclose(fp);
            return -1;
        }
        printf("写入成功\n");
        fclose(fp);
        return -1; // 取消订单
    }
    else
    {
        pthread_mutex_unlock(&keep_lock);
        fprintf(fp, "on\n");
        fclose(fp);
        return -1;
    }
}

int ORDER_str(char *buf, char *number, char *commodity_num, char *num)
{
    char *p = strtok(buf, ",");
    int i = 0;
    while (p != NULL)
    {
        printf("p1 = %s\n", p);
        if (i == 0)
            ;

        else if (i == 1)
            strcpy(number, p);
        else if (i == 2)
        {
            if (strcmp(p, "end") == 0)
            {
                printf("订单创建成功\n");
                return 1;
            }
            if (strcmp(p, "cancel") == 0)
                return 3;
            strcpy(commodity_num, p);
        }
        else if (i == 3)
        {
             strcpy(num, p);
        } 
        i++;
        p = strtok(NULL, ",");
    }
    if(strcmp(commodity_num, "ok") == 0)
    {
        return 2;
    }
    return 0;
}

int order_str()
{
    FILE *fp = fopen("/root/kkcode/second_item/Data/order/Simple_order.txt", "r+");
    if (fp == NULL)
    {
        printf("打开文件失败\n");
        return -1;
    }
    FILE *fd = fopen("/root/kkcode/second_item/Data/order/order.txt", "a+");
    if (fp == NULL)
    {
        printf("打开文件失败\n");
        return -1;
    }
    char buf[255] = {0};
    while (1)
    {
        char *arr=fgets(buf, sizeof(buf), fp);
        if (arr == NULL)
        {
            break;
        }
        printf("buf = %s\n", buf);
        if (strstr(buf, "ok"))
        {
            printf("订单创建成功\n");
            time_t current_time = time(NULL);
            char *time_str = malloc(sizeof(char) * 20);
            strftime(time_str, 20, "%Y-%m-%d %H:%M:%S", localtime(&current_time));
            char *p = strtok(buf, ",");
            char order_tai[128] = {0};
            char order_id[128] = {0};
            char order_total[128] = {0};
            int i = 0;
            while (p != NULL)
            {
                printf("p = %s\n", p);
                if (i == 0)
                {
                    strcpy(order_tai, p);
                }
                if (i == 1)
                {
                    strcpy(order_id, p);
                }
                else if (i == 2)
                {
                    strcpy(order_total, p);
                    
                }
                p = strtok(NULL, ",");
                i++;
            }
            //去掉换行符
            order_total[strlen(order_total) - 1] = '\0';

            fprintf(fd, "订单号:%s,状态:%s,总价%s,交易时间：%s\n", order_id, order_tai, order_total, time_str);
        }
        else if (strstr(buf, "cancel"))
        {
            printf("取消订单\n");
        }
        else
        {
            char number[128] = {0};
            char num[128] = {0};
            char *p = strtok(buf, ",");
            int i = 0;
            while (p != NULL)
            {
                printf("p = %s\n", p);
                if (i == 1)
                {
                    strcpy(number, p);
                }
                else if (i == 2)
                {
                    strcpy(num, p);
                }
                p = strtok(NULL, ",");
                i++;
            }
            commodity_list_p ptr = commodity_head->next;
            while (ptr != NULL)
            {

                if (strstr(ptr->commodity->commodity_id, number))
                {
                    
                    ptr->commodity_num = ptr->commodity_num - atoi(num);
                    //printf("商品数量为%d\n", ptr->commodity_num);
                }
                ptr = ptr->next;
            }
        }
    }
    fclose(fp);
    fclose(fd);
    return 0;
}
